<template>
	<xn-form-container
		:title="formData.id ? '编辑文件' : '新增文件'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="上传文件：" name="fileDescription">
				<a-input v-model:value="uploadedFileName" placeholder="待上传文件" allow-clear :disabled="true" style="margin-bottom: 10px; display: none; "/>
				<a-spin :spinning="uploadLoading">
					<a-upload-dragger :show-upload-list="false" :custom-request="customRequestLocal">
						<p class="ant-upload-drag-icon">
							<inbox-outlined></inbox-outlined>
						</p>
						<p class="ant-upload-text">单击或拖动文件到此区域进行上传</p>
						<p class="ant-upload-hint">支持单个上传</p>
					</a-upload-dragger>
				</a-spin>
			</a-form-item>
			<a-form-item label="文件描述：" name="content" v-if="sendType === 'TXT'">
				<a-textarea
					v-model:value="formData.fileDescription"
					placeholder="请输入文件描述"
					:auto-size="{ minRows: 6, maxRows: 6 }"
				/>
			</a-form-item>

		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="fileManagementForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import fileManagementApi from '@/api/biz/fileManagementApi'
	import fileApi from "@/api/dev/fileApi";
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 默认是关闭状态
	const activeKey = ref('Local')
	const uploadLoading = ref(false)

	// 发送文本方式
	const sendType = ref('TXT')

	//上传文件名称
	const uploadedFileName = ref('');

	//上传文件编码
	const fileCode = ref('');

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		uploadedFileName: [required('请拖动文件上传')],
		fileDescription: [required('请输入文件描述')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			formData.value.fileName = uploadedFileName;
			formData.value.fileCode = fileCode.value;
			const formDataParam = cloneDeep(formData.value)
			fileManagementApi
				.fileManagementSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 上传本地文件
	const customRequestLocal = (data) => {

		fileCode.value = Date.now();
		console.log(fileCode.value)
		uploadedFileName.value = data.file.name;

		// uploadLoading.value = true
		// const fileData = new FormData()
		// fileData.append('file', data.file)
		// fileApi
		// 	//todo：这边的文件上传方法要自己写
		// 	.fileUploadLocalReturnUrl(fileData)
		// 	.then(() => {
		// 		emit('successful')
		// 	})
		// 	.finally(() => {
		// 		// 获取上传的文件名称
		// 		// 将文件名称赋值给 uploadedFileName
		// 		uploadedFileName.value = data.file.name;
		// 		uploadLoading.value = false
		// 	})
	};

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
